import sqlite3
import os
import re
from urllib.request import urlopen
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

#从数据库读id
con=sqlite3.connect("../book.db")
cur=con.cursor()
cur.execute("select * from catalog")
rows = cur.fetchall()
id="000000"
tt=""
n=0
for row in rows:
    tt=row[0]
if tt!="":
    num=tt.replace('0','')
    n=eval(num)
    nn = len(id) - len(num)
    id="000000"[0:nn]+num





#爬虫：
class book:
    id = ""
    title = ""
    pic = ""
    author = ""
    type = ""
    publish = ""
    intro = []
    rating = 0
    detailUrl = ""

    def __init__(self, id_t, title_t, pic_t, author_t, type_t, publish_t, intro_t, rating_t, detailUrl_t):
        book.id = id_t
        book.title = title_t
        book.pic = pic_t
        book.author = author_t
        book.type = type_t
        book.publish = publish_t
        book.intro = intro_t
        book.rating = rating_t
        book.detailUrl = detailUrl_t

dirname = 'pintuan'
if not os.path.isdir(dirname):
    os.mkdir(dirname)
os.chdir(dirname)
dirname = os.getcwd()

#获取全部动态加载的数据https://blog.csdn.net/weixin_44706011/article/details/103550778
url = r'https://weread.qq.com/web/category/all'
browser = webdriver.Chrome()
browser.get(url)#打开微信读书总榜200
for i in range(10):
    browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')  # 将滚动条滑动到最底部
    time.sleep(1)
ul=browser.find_elements(By.CLASS_NAME, "wr_bookList_item_link")
content=[]
for li in ul:
    url=li.get_attribute("href")
    content.append(url)

for name in content:
    detailUrl =  name
    with urlopen(detailUrl) as fp:
        detail = fp.read().decode()
    detail.replace(u'\xa0', u' ')
    pattern = r'<meta name="keywords" content="(.+?)" />'
    tt = re.findall(pattern, detail)
    temp = tt[0].split(',')
    title = temp[0]
    author = temp[1]
    type = temp[2]
    publish = temp[4]
    pattern = r'<img src="(.+?)" alt="书籍封面" class="wr_bookCover_img">'
    tt = re.findall(pattern, detail)
    imgUrl = tt[0]
    pattern = r'<p class="introDialog_content_intro_para">(.+?)</p>'
    intro1 = re.findall(pattern, detail)
    intro=''.join(intro1)
    pattern = r"微信读书推荐值((?:.|\n)*?)</span>"
    aa = re.findall(pattern, detail)
    if aa[0][33]=="%":
        a1 = aa[0][29:33]
        a1.replace('%', '')
        recommd_rate = float(a1)
    else:
        recommd_rate = 0
    print(title,recommd_rate)
    n += 1
    num=str(n)
    nn=len("000000")-len(num)
    id = "000000"[0:nn] + num
    B1 = book(id,title, imgUrl, author, type, publish, intro, recommd_rate, detailUrl)
    #print("INSERT INTO catalog VALUES ('"+B1.id+"','"+B1.title+"','"+B1.pic+"','"+B1.author+"','"+B1.intro+"','"+B1.type+"',"+str(B1.rating)+",'"+B1.publish+"','"+B1.detailUrl+"');")
    cur.execute("INSERT INTO catalog VALUES ('"+B1.id+"','"+B1.title+"','"+B1.pic+"','"+B1.author+"','"+B1.intro+"','"+B1.type+"',"+str(B1.rating)+",'"+B1.publish+"','"+B1.detailUrl+"');")
con.commit()
con.close()
print('信息读取完毕')


